#ifndef MYDATETIMEEDIT_H
#define MYDATETIMEEDIT_H
#include <QTimeEdit>
#include <QMouseEvent>

#include <QWidget>
#include <QCalendarWidget>
#include <QTimeEdit>
#include <QVBoxLayout>
#include <QPushButton>
#include <QDateTime>
#include <QDialog>

class CustomDateTimePopup : public QDialog
{
    Q_OBJECT
public:
    QCalendarWidget* calendar;
    QTimeEdit* timeEdit;

    CustomDateTimePopup(QWidget *parent = nullptr) : QDialog(parent)
    {
        QVBoxLayout *layout = new QVBoxLayout(this);

        // 添加日历部件
        calendar = new QCalendarWidget(this);
        layout->addWidget(calendar);

        // 添加时间编辑部件
        timeEdit = new QTimeEdit(this);
        layout->addWidget(timeEdit);

        // 添加确定按钮
        QPushButton *okButton = new QPushButton("确定", this);
        connect(okButton, &QPushButton::clicked, this, &CustomDateTimePopup::accept);
        layout->addWidget(okButton);
    }

    QDateTime dateTime() const
    {
        return QDateTime(calendar->selectedDate(), timeEdit->time());
    }

signals:
    void accepted();

private slots:
    void accept()
    {
        emit accepted();
        hide();
    }
};

class MyDateTimeEdit : public QDateTimeEdit
{
    Q_OBJECT
public:
    MyDateTimeEdit(QWidget *parent = nullptr) : QDateTimeEdit(parent)
    {
        setCalendarPopup(false);
    }

protected:
    void mousePressEvent(QMouseEvent *event) override
    {
        if (event->button() == Qt::LeftButton)
        {
            CustomDateTimePopup popup(this);
            connect(&popup, &CustomDateTimePopup::accepted, this, [this, &popup]() {
                setDateTime(popup.dateTime());
            });
            popup.exec();
        }
        QDateTimeEdit::mousePressEvent(event);
    }
};

#endif // MYDATETIMEEDIT_H
